#include "Col.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "Token.h"

#include "Math.h"
#include "String.h"
#include "ET_stdio.h"

extern int gDebug;

const int cDoubleClick= 500;  // 0.5 sec

static _InitTermCnt itc;

//---- Token -------------------------------------------------------------------

Token::Token() : Code(-1), Flags(eFlgNone)
{
}

Token::Token(EventCodes ec, EventFlags f, Point p) : Code(ec), Flags(f), Pos(p)
{
}

Token::Token(const Token &t) : Code(t.Code), Flags(t.Flags), Pos(t.Pos), At(t.At)
{
}

bool Token::DoubleClick(const Token &t) const
{
	int t1= t.Pos.x-Pos.x, t2= t.Pos.y-Pos.y;
	long t3= t.At-At;
	if (t.IsMouseButton())
		if (t.Code == Code && Math::Abs(t1) < 2 && Math::Abs(t2) < 2
							&& Math::Abs(t3) < cDoubleClick)
			return TRUE;
	return FALSE;
}

char Token::MapToAscii() const
{
	return (Code & 0x7f) + 0x60;
}

Point Token::CursorPoint() const
{
	switch (CursorDir()) {
	case eCrsUp:
		return Point(0,-1);
	case eCrsDown:
		return Point(0,1);
	case eCrsLeft:
		return Point(-1,0);
	case eCrsRight:
		return Point(1,0);
	default:
		return gPoint0;
	}
}

void Token::Dump() const
{
	char *c= "???";

	if (gDebug == 0)
		return;

	int code= Code;
	switch(Code) {
	case eEvtLocMove:
		c= "LocMove";
		break;
	case eEvtEnter:
		c= "Enter";
		break;
	case eEvtExit:
		c= "Exit";
		break;
	case eEvtLocMoveBut:
		c= "LocMoveBut";
		break;
	case eEvtLeftButton:
		c= "LeftButton";
		break;
	case eEvtMiddleButton:
		c= "MiddleButton";
		break;
	case eEvtRightButton:
		c= "RightButton";
		break;
	default:
		if (Code >= eEvtPfkFirst && Code <= eEvtPfkLast) {
			c= "PfkFirst";
			code-= eEvtPfkFirst;
		} if (Code >= eEvtAscFirst && Code <= eEvtAscLast)
			c= "AscFirst";
		if (Code >= eEvtMetaFirst && Code <= eEvtMetaLast)
			c= "MetaFirst";
		break;
	}
	fprintf(stderr, "c:%s(%d) f:%04x x:%d y:%d\n", c, code, Flags, Pos.x, Pos.y);
}

